#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a simple backup of the Bacula build directory
#   then verify the catalog.           
#
TestName="verify-vol-test"
JobName=VerifyVol
. scripts/functions

require_linux

cwd=`pwd`
scripts/cleanup
scripts/copy-test-confs
echo "${cwd}/build" >${cwd}/tmp/file-list
rm -f ${cwd}/build/sparsefile
#
# Add this back when verify can handle sparse files
#  the same way that backup does
#
#dd if=${cwd}/build/configure of=${cwd}/build/sparsefile bs=1 count=1 seek=10M

change_jobname NightlySave $JobName
start_test

# Create list of files being backed up and verified to compare afterwards
files_listing=${cwd}/tmp/files
find ${cwd}/build -print > ${files_listing}

# enable the Verified message in the console
sed '/console =*/ s/, !verified//g' $conf/bacula-dir.conf > $tmp/1
mv $tmp/1 $conf/bacula-dir.conf
sed '/director =*/ s/, !verified//g' $conf/bacula-fd.conf > $tmp/1
mv $tmp/1 $conf/bacula-fd.conf

cat <<END_OF_DATA >tmp/bconcmds
@output /dev/null
messages
@$out tmp/log1.out
setdebug level=1 storage=File sd trace=1
label storage=File volume=TestVolume001
run job=$JobName yes
wait
messages
run job=MonsterSave storage=File yes
wait
messages
@# 
@# now do a verify volume
@#
@$out ${cwd}/tmp/log2.out
setdebug level=200 dir tags=sql trace=1
run job=VerifyVolume yes
wait
messages
@$out ${cwd}/tmp/log3.out
run job=VerifyVolume level=DiskToCatalog jobid=1
yes
wait
messages
@$out ${cwd}/tmp/log4.out
run job=VerifyDiskToCatalog yes
wait
messages
quit
END_OF_DATA

#
#  ***FIXME***
# Note, add sparse file backup and check that checksum is
#  correct.  Bug #1140
#  create sparse file
#  dd if=/dev/zero of=sparsefile bs=1 count=0 seek=10M
#

nb=0
print_info()
{
    nb=`expr $nb + 1`
    if [ $nb = 50 ]; then
        print_debug "Output truncated after 50 errors"

    elif [ $nb -lt 50 ]; then
        print_debug $1
    fi
}

run_bacula


check_for_zombie_jobs storage=File
stop_bacula

# Turn off verbose file listing, rerun verify jobs and check if files are not listed as expected
sed '/console =*/ s/$/, !verified/g' $conf/bacula-dir.conf > $tmp/1
mv $tmp/1 $conf/bacula-dir.conf
sed '/director =*/ s/$/, !verified/g' $conf/bacula-fd.conf > $tmp/1
mv $tmp/1 $conf/bacula-fd.conf

cat <<END_OF_DATA >tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log5.out
reload
setdebug level=200 dir tags=sql trace=1
run job=VerifyVolume yes
wait
messages
@$out ${cwd}/tmp/log6.out
run job=VerifyVolume level=DiskToCatalog jobid=1
yes
wait
messages
@$out ${cwd}/tmp/log7.out
run job=VerifyDiskToCatalog yes
wait
messages
quit
END_OF_DATA

run_bacula

while IFS= read -r line <&3; do
   grep "Veryfying: ${line}" tmp/log2.out > /dev/null
   if [ $? -ne 0 ]; then
      estat=1
      print_info "Did not found ${line} file in verified files log!"
      print_info "see: ${cwd}/tmp/log2.out"
   fi
   grep "Veryfying: ${line}" tmp/log3.out > /dev/null
   if [ $? -ne 0 ]; then
      estat=1
      print_info "Did not found ${line} file in verified files log!"
      print_info "see: ${cwd}/tmp/log3.out"
   fi
   grep "Veryfying: ${line}" tmp/log4.out > /dev/null
   if [ $? -ne 0 ]; then
      estat=1
      print_info "Did not found ${line} file in verified files log!"
      print_info "see: ${cwd}/tmp/log4.out"
   fi
done 3< ${files_listing}

grep "^  Termination: *Backup OK" tmp/log1.out 2>&1 >/dev/null
bstat=$?
grep "^  Termination: *Verify OK" tmp/log2.out 2>&1 >/dev/null
rstat=$?
if test $rstat=0; then
   grep "^  Termination: *Verify OK" tmp/log3.out 2>&1 >/dev/null
   rstat=$?
fi
grep "^  Termination: *Verify OK" tmp/log4.out 2>&1 >/dev/null
if [ $? != 0 ]; then
    print_info "ERROR: Verify job in error in $tmp/log4.out"
    rstat=1
fi

while IFS= read -r line <&3; do
   grep "Veryfying: ${line}" tmp/log5.out > /dev/null
   if [ $? -eq 0 ]; then
      estat=1
      print_info "Found ${line} file in verified files log, no files should be listed now!"
      print_info "see: ${cwd}/tmp/log5.out"
   fi
   grep "Veryfying: ${line}" tmp/log6.out > /dev/null
   if [ $? -eq 0 ]; then
      estat=1
      print_info "Found ${line} file in verified files log, no files should be listed now!"
      print_info "see: ${cwd}/tmp/log6.out"
   fi
   grep "Veryfying: ${line}" tmp/log7.out > /dev/null
   if [ $? -eq 0 ]; then
      estat=1
      print_info "Found ${line} file in verified files log, no files should be listed now!"
      print_info "see: ${cwd}/tmp/log7.out"
   fi
done 3< ${files_listing}

check_for_zombie_jobs storage=File
stop_bacula

end_test
